/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alag.ci.textanalysis.impl;
import java.util.*;
import com.alag.ci.*;
import com.alag.ci.impl.*;
import com.alag.ci.textanalysis.MetaDataExtractor;

/**
 *
 * @author sczbbx
 */
public class SimpleMetaDataExtractor implements MetaDataExtractor {
    
    private Map<String, Long> idMap = null;
    private Long currentId = null;
    
    public SimpleMetaDataExtractor() {
        this.idMap = new HashMap<String, Long>();
        this.currentId = new Long(0);
    }
    
    public MetaDataVector extractMetaData(String title, String body) {
        MetaDataVector titleMDV = getMetaDataVector(title);
        MetaDataVector bodyMDV = getMetaDataVector(body);
        return titleMDV.add(bodyMDV);
    }
    
    private Long getTokenId(String token) {
        Long id = this.idMap.get(token);
        if (id == null) {
            id = this.currentId ++;
            this.idMap.put(token, id);
        }
        return id;
    }

    protected MetaDataVector getMetaDataVector(String text) {
        Map<String, Integer> keywordMap = new HashMap<String, Integer>();
        StringTokenizer st = new StringTokenizer(text);
        while (st.hasMoreTokens()) {
            String token = normalizeToken(st.nextToken());
            if (acceptToken(token)) {
                Integer count = keywordMap.get(token);
                if (count == null) {
                    count = new Integer(0);
                }
                count ++;
                keywordMap.put(token, count);
            }
        }
        MetaDataVector mdv = createMetaDataVector(keywordMap);
        return mdv;
    }

    protected boolean acceptToken(String token) {
        return true;
    }

    protected String normalizeToken(String token) {
        String normalizedToken = token.toLowerCase().trim();
        if ( (normalizedToken.endsWith(".")) || normalizedToken.endsWith(",") ) {
            int size = normalizedToken.length();
            normalizedToken = normalizedToken.substring(0, size - 1);
        }
        return normalizedToken;
    }

    protected MetaDataVector createMetaDataVector(Map<String, Integer> keywordMap) {
        return new MetaDataVectorImpl(keywordMap);
    }
    
}
